<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\common\model\mycurrency\LeaseOrder;
use app\common\model\mycurrency\Pay;
use app\common\model\WechatChapterOrder;
use think\Db;
use think\Exception;

/**
 * 免押接口
 */
class WechatChapter extends Api
{
    protected $noNeedLogin = ['chapterNotify'];
    protected $noNeedRight = '*';

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 获得免押参数
    */
    public function getChapterCode(){

//        $param = $this->request->param();
//        $allowOrderType = [
//            Pay::ORDER_TYPE_YUE,//用户余额充值订单
//            Pay::ORDER_TYPE_YAJIN,//租赁订单押金支付
//            Pay::ORDER_TYPE_ZULINFEIYONG,//租赁订单租赁费用支付
//        ];
//        $allowOrderType = join(',', $allowOrderType);
//        $rule = [
//            ['order_id', 'require|>:0', '请填写订单id|订单id必须是大于0的数字'],
//            ['order_type', "require|in:{$allowOrderType}", '请填写订单类型|无效的订单类型'],
//            ['pay_type', 'require|in:wechat,alipay', '请填写支付类型|无效的支付类型']
//        ];
//        $check = $this->validate($param, $rule);
//        if ($check !== true) {
//            $this->error($check);
//        }
//        Db::startTrans();
//        try {
//
//            Db::commit();
//        } catch (Exception $e) {
//            Db::rollback();
//            $this->error($e->getMessage());
//        }
//        $this->success('获取支付代码成功');
    }

    /**
     * 微信支付分成功后回调
     */
    public function chapterNotify()
    {
        trace("========================微信支付分成功后回调==============================");
        $wechatChapter = \app\common\library\WechatChapter::getInstance();
        $handelFunction = function ($data) {
            $outOrderNo = $data['out_order_no'];
            WechatChapterOrder::authorizeSuccess($outOrderNo);
        };
        $wechatChapter->handelNotify($handelFunction);
    }

}
